//package com.lsq.tucao.user.interceptor;
//
//import com.auth0.jwt.exceptions.AlgorithmMismatchException;
//import com.auth0.jwt.exceptions.SignatureVerificationException;
//import com.auth0.jwt.exceptions.TokenExpiredException;
//import com.lsq.tucao.user.response.JsonResult;
//import com.lsq.tucao.user.utils.JWTUtils;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.shiro.authc.AuthenticationToken;
//import org.apache.shiro.web.filter.authc.AuthenticatingFilter;
//import org.apache.shiro.web.util.WebUtils;
//
//import javax.servlet.ServletRequest;
//import javax.servlet.ServletResponse;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
///**
// * @Author linshouqiang
// * @Description JWT 自定义拦截器
// * @Date 2020/11/25 12:14
// * @Version 1.0
// */
//@Slf4j
//public class JwtInterceptor extends AuthenticatingFilter {
//
//    @Override
//    protected AuthenticationToken createToken(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception {
//        return null;
//    }
//
//    /**
//     * 在这里拦截所有请求
//     */
//    @Override
//    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
//        log.info("请求被拦截。。。");
//        HttpServletRequest httpServletRequest = WebUtils.toHttp(request);
//        String token = httpServletRequest.getHeader("token");
//
//        if (token != null && !"".equals(token)) {
//            try {
//                log.info("token:{}",token);
//                this.executeLogin(request, response);
//                return true;
//            } catch (SignatureVerificationException e) {
//                log.error("无效签名", e);
//            } catch (TokenExpiredException e) {
//                log.error("token过期", e);
//            } catch (AlgorithmMismatchException e) {
//                log.error("token算法不一致", e);
//            } catch (Exception e) {
//                log.error("令牌认证异常", e);
//            }
//            return false;
//        } else {
//            String httpMethod = httpServletRequest.getMethod();
//            String requestURI = httpServletRequest.getRequestURI();
//            log.error("请求:{}的Token为空 请求类型:{}", requestURI, httpMethod);
//            return false;
//        }
//
//    }
//
//    /**
//     * token存在，开始进行令牌认证
//     */
//    @Override
//    protected boolean executeLogin(ServletRequest request, ServletResponse response) throws Exception {
//        log.info("token存在,开始进行令牌认证。。。");
//        String token = WebUtils.toHttp(request).getHeader("token");
//        //令牌认证
//        JWTUtils.verify(token);
//        log.info("令牌认证通过。。。");
//        return true;
//    }
//
//    /**
//     * 请求失败,对失败的请求拦截,请求终止,不进行转发直接返回客户端拦截结果
//     */
//    @Override
//    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
//        log.info("请求失败,被拦截。。。");
//        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
//        httpServletResponse.setContentType("application/json; charset=utf-8");
//        httpServletResponse.setCharacterEncoding("UTF-8");
//        httpServletResponse.setStatus(401);
//
//        String result = new JsonResult("HttpServletResponse:" + httpServletResponse, 401, "请求失败!").toString();
//        httpServletResponse.getWriter().print(result);
//
//        return false;
//    }
//}
